<?php
namespace Home\Controller;

use Org\Util\Date;

use Think\Controller;

class IndexController extends BaseController {


    public function index() {
        $banner = M('banner');
        $banner_list = $banner->select();

        $this->assign('banner_list1', $banner_list[0]);
        $this->assign('banner_list2', $banner_list[1]);
        $this->assign('banner_list3', $banner_list[2]);
        $this->assign('banner_list4', $banner_list[3]);


        $article = M('article');

        $PJID = 15;
        $PJ_list = $article->where("article_status=2 AND  article_column=$PJID ")->order('article_id desc')->limit(3)->select();
        $this->assign('PJID', $PJID);
        $this->assign('PJ_list', $PJ_list);

        $PCID = 16;
        $PC_list = $article->where("article_status=2 AND article_column=$PCID ")->order('article_id desc')->limit(4)->select();
        $this->assign('PCID', 16);
        $this->assign('PC_list', $PC_list);

        $WBID = 3;
        $WB_list = $article->where("article_status=2 AND article_column in (17,18) ")->order('article_id desc')->limit(3)->select();
        $this->assign('WBID', 3);
        $this->assign('WB_list', $WB_list);


        $this->display("index");

    }


    public function CPK() {

        $id = $_GET["id"];
        $p = $_GET["P"] ? $_GET["P"] : 1;
        $pre = 10;
        $start = ($p - 1) * $pre;
        if ($id) {

            //查找栏目名称
            $cpk = M('column');
            $column = $cpk->where("id=$id")->find();
            $this->assign('column', $column);

            //查找栏目文章
            $article = M('article');
            $article_list = $article->where("article_status=2 AND article_column=$id")->order('article_id desc')->limit($start, $pre)->select();
            $this->assign('article_list', $article_list);

            $count = $article->where("article_status=2 AND article_column=$id")->count();
            $pcount = ceil($count / $pre);

        } else {
            //没有特定ID模块
            //产品库ID=1
            $id = 1;
            $cpk = M('column');
            $column = $cpk->where("id=$id")->find();
            $this->assign('column', $column);

            //查到所有产品库的子ID
            $article = M('article');
            $article_list = $article->where("article_status=2 AND article_column in (6,7,8,9,10,11,12,13,14)")->order('article_id desc')->limit($start, $pre)->select();
            $this->assign('article_list', $article_list);

            $count = $article->where("article_status=2 AND article_column in (6,7,8,9,10,11,12,13,14)")->count();
            $pcount = ceil($count / $pre);

        }

        $this->assign('pcount', $pcount);
        $this->assign('p', $p);
        $this->assign('id', $id);

        $this->display("list_cpk");
    }


    public function PJPC() {
        $id = $_GET["id"];
        $p = $_GET["P"] ? $_GET["P"] : 1;
        $pre = 10;
        $start = ($p - 1) * $pre;
        if ($id) {

            //查找栏目名称
            $cpk = M('column');
            $column = $cpk->where("id=$id")->find();
            $this->assign('column', $column);

            //查找栏目文章
            $article = M('article');
            $article_list = $article->where("article_status=2 AND article_column=$id")->order('article_id desc')->limit($start, $pre)->select();
            $this->assign('article_list', $article_list);

            $count = $article->where("article_status=2 AND article_column=$id")->count();
            $pcount = ceil($count / $pre);


        } else {
            //没有特定ID模块
            //评级评测ID=2
            $id = 2;
            $cpk = M('column');
            $column = $cpk->where("id=$id")->find();
            $this->assign('column', $column);

            //查到所有产品库的子ID
            $article = M('article');
            $article_list = $article->where("article_status=2 AND article_column in (15,16)")->order('article_id desc')->limit($start, $pre)->select();
            $this->assign('article_list', $article_list);

            $count = $article->where("article_status=2 AND article_column in (15,16)")->count();
            $pcount = ceil($count / $pre);

        }

        $this->assign('pcount', $pcount);
        $this->assign('p', $p);
        $this->assign('id', $id);
        $this->display("list_pjpc");
    }


    public function WBZY() {
        $id = $_GET["id"];
        $p = $_GET["P"] ? $_GET["P"] : 1;
        $pre = 10;
        $start = ($p - 1) * $pre;
        if ($id) {

            //查找栏目名称
            $cpk = M('column');
            $column = $cpk->where("id=$id")->find();
            $this->assign('column', $column);

            //查找栏目文章
            $article = M('article');
            $article_list = $article->where("article_status=2 AND article_column=$id")->order('article_id desc')->limit($start, $pre)->select();
            $this->assign('article_list', $article_list);

            $count = $article->where("article_status=2 AND article_column=$id")->count();
            $pcount = ceil($count / $pre);

        } else {
            //没有特定ID模块
            //外包资源ID=3
            $id = 3;
            $cpk = M('column');
            $column = $cpk->where("id=$id")->find();
            $this->assign('column', $column);

            //查到所有产品库的子ID
            $article = M('article');
            $article_list = $article->where("article_status=2 AND article_column in (17,18)")->order('article_id desc')->limit($start, $pre)->select();
            $this->assign('article_list', $article_list);

            $count = $article->where("article_status=2 AND article_column in (17,18)")->count();
            $pcount = ceil($count / $pre);
        }

        $this->assign('pcount', $pcount);
        $this->assign('p', $p);
        $this->assign('id', $id);
        $this->display("list_wbzy");
    }


    public function XWGS() {
        $id = $_GET["id"];
        $p = $_GET["P"] ? $_GET["P"] : 1;
        $pre = 10;
        $start = ($p - 1) * $pre;
        if ($id) {

            //查找栏目名称
            $cpk = M('column');
            $column = $cpk->where("id=$id")->find();
            $this->assign('column', $column);

            //查找栏目文章
            $article = M('article');
            $article_list = $article->where("article_status=2 AND article_column=$id")->order('article_id desc')->limit($start, $pre)->select();
            $this->assign('article_list', $article_list);

            $count = $article->where("article_status=2 AND article_column=$id")->count();
            $pcount = ceil($count / $pre);

        } else {
            //没有特定ID模块
            //新闻故事ID=4
            $id = 4;
            $cpk = M('column');
            $column = $cpk->where("id=$id")->find();
            $this->assign('column', $column);

            //查到所有产品库的子ID
            $article = M('article');
            $article_list = $article->where("article_status=2 AND article_column in (19,20,21)")->order('article_id desc')->limit($start, $pre)->select();
            $this->assign('article_list', $article_list);

            $count = $article->where("article_status=2 AND article_column in (19,20,21)")->count();
            $pcount = ceil($count / $pre);
        }

        $this->assign('pcount', $pcount);
        $this->assign('p', $p);
        $this->assign('id', $id);
        $this->display("list_xwgs");
    }

    public function YWXQ() {
        $islogin = $this->islogin();
        if ($islogin == false) {
            $this->display("Public:login");
            exit;
        }


        $p = $_GET["p"] ? $_GET["p"] : 1;
        $pre = 6;
        $start = ($p - 1) * $pre;


        $feedback = M('feedback');
        $feedback_list = $feedback->order('id desc')->limit($start, $pre)->select();
        $this->assign('feedback_list', $feedback_list);

        $count = $feedback->count();
        $pcount = ceil($count / $pre);

        $this->assign('pcount', $pcount);
        $this->assign('p', $p);
        $this->display("list_ywxq");


    }

    public function MSZY() {
    	 $id = $_GET["id"];
         
    	 $p = $_GET["P"] ? $_GET["P"] : 1;
         $pre = 16;
         $start = ($p - 1) * $pre;
         if ($id>2) {

            //查找栏目名称
            $mszy = M('column_pic');
            $column = $mszy->where("id=$id")->find();
            $this->assign('column', $column);

            //查找图片
            $pic = M('pic');
            $pic_list = $pic->where("pic_status=0 AND pic_column=$id")->order('id desc')->limit($start, $pre)->select();
            $this->assign('pic_list', $pic_list);

            $count = $pic->where("pic_status=0 AND pic_column=$id")->count();
            $pcount = ceil($count / $pre);

        } elseif($id==2){
        	
            $cpk = M('column');
            $column["column_name"] = "3D";
            $this->assign('column', $column);

            //查到所有产品库的子ID
            $pic = M('pic');
            $pic_list = $pic->where("pic_status=0 and pic_column in (8,9,10,11)")->order('id desc')->limit($start, $pre)->select();
            $this->assign('pic_list', $pic_list);

            $count = $pic->where("pic_status=0 and pic_column in (8,9,10,11)")->count();
            $pcount = ceil($count / $pre);
        	
        	
        
        }elseif($id==1){
        	//没有特定ID模块
           
            $cpk = M('column');
            $column["column_name"] = "2D";
            $this->assign('column', $column);

            //查到所有产品库的子ID
            $pic = M('pic');
            $pic_list = $pic->where("pic_status=0 and pic_column in (3,4,5,6,7)")->order('id desc')->limit($start, $pre)->select();
            $this->assign('pic_list', $pic_list);

            $count = $pic->where("pic_status=0 and pic_column in (3,4,5,6,7)")->count();
            $pcount = ceil($count / $pre);
        	
        	
        	
        }elseif($id==0) {
            //没有特定ID模块
            $id = 1;
            $cpk = M('column');
            $column["column_name"] = "全部";
            $this->assign('column', $column);

            //查到所有产品库的子ID
            $pic = M('pic');
            $pic_list = $pic->where("pic_status=0")->order('id desc')->limit($start, $pre)->select();
            $this->assign('pic_list', $pic_list);

            $count = $pic->where("pic_status=0")->count();
            $pcount = ceil($count / $pre);

        }

        $this->assign('pcount', $pcount);
        $this->assign('p', $p);
        $this->assign('id', $id);
        $this->display("list_mszy");
    }

    public function add_feedback() {
        $title = $_GET["title"];
        $content = $_GET["text"];
        $created_time = date("Y-m-d H:i:s");

        $feedback = M('feedback');
        $data = $feedback->create();

        $data["title"] = $title;
        $data["content"] = $content;
        $data["created_time"] = $created_time;
        $result = $feedback->add($data);

        if ($result !== false) {
            $this->success('反馈新增成功！', '/index.php/Home/Index/YWXQ');
        } else {
            $this->error("反馈新增失败", '/index.php/Home/Index/YWXQ');
        }

    }


    public function login() {
        $account = $_POST["account"];
        $psd = $_POST["psd"];

        if ($account && $psd) {
            $user = M('user');
            $condition["account"] = $account;
            $condition["password"] = $psd;


            $user_result = $user->where($condition)->find();

            if ($user_result) {
//					if($user_result["status"]==0){
//						$this->error('账户禁用，联系管理员！','/index.php/Home/Index');
//					}else{
                //把ID和账户名称写入SESSION
                $_SESSION['index_account'] = $user_result['account'];
                $_SESSION['index_id'] = $user_result['id'];
                $_SESSION['index_power'] = $user_result['power'];
                $_SESSION['index_name'] = $user_result['name'];

                $this->success('登录成功！', '/index.php/Home/Index');
//					}

            }
        }
        //去查找是否登录成功
        $this->error('登录失败！', '/index.php/Home/Index');
    }

    public function loginout() {
        session_destroy();
        $this->success('退出成功！', '/index.php/Home/Index');
    }

    public function reg() {
        $account = $_POST["account"];
        $psd = $_POST["psd"];
        $email = $_POST["email"];
        $name = $_POST["name"];


        $user = M('user');
        if ($account) {
            $condition["account"] = $account;

            $isuser_result = $user->where($condition)->find();
            if ($isuser_result) {
                $this->error('该账号已注册！', '/index.php/Home/Index');
            } else {
                $data = $user->create();
                $data["psd"] = $psd;
                $data["email"] = $email;
                $data["name"] = $name;
                $data["account"] = $account;
                $data["created_time"] = date("Y-m-d H:i:s");
                $data["power"] = 1;
                $result = $user->add($data);
                if ($result !== false) {

                    $_SESSION['index_account'] = $account;
                    $_SESSION['index_id'] = $result;
                    $_SESSION['index_power'] = 1;
                    $_SESSION['index_name'] = $name;
                    $this->success('注册成功！', '/index.php/Home/Index');
                } else {
                    $this->error('注册失败！', '/index.php/Home/Index');
                }
            }
        }


    }

}